﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class SocioRepository:MasterRepository,ISocioRepository
    {
        public List<SOCIO> GetAllFromSocio()
        {
            var query = from s in context.SOCIOs.Include("TIPO_SOCIO")
                        select s;
            return query.ToList();
        }

        public List<SOCIO> GetAllFromSocioByDescripcion(string descripcion)
        {
            var query = from s in context.SOCIOs.Include("TIPO_SOCIO")
                        select s;
            if (descripcion != null && descripcion != string.Empty && descripcion != "")
            {
                query = from s in context.SOCIOs
                        where s.Ruc.Contains(descripcion) ||
                                  s.RazonSocial.ToUpper().Contains(descripcion)
                        select s;
            }
            return query.ToList();
        }

        public SOCIO GetFromSocioById(int id)
        {
            var query = from s in context.SOCIOs
                        where s.IdSocio.Equals(id)
                        select s;
            return query.SingleOrDefault();
        }

        

        public void AddSocio(SOCIO socio)
        {
            socio.TIPO_SOCIO = null;
            socio.TRABAJADORs = null;
            
            context.SOCIOs.Add(socio);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void UpdateSocio(SOCIO socio)
        {
            socio.TRABAJADORs = null;
            socio.TIPO_SOCIO = null;
            context.Entry(socio).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void RemoveSocio(int id)
        {
            var existe = context.SOCIOs.Find(id);

            if (existe != null)
            {
                context.SOCIOs.Remove(existe);
                context.SaveChanges();
            }
        }


        public bool ExisteDuplicidad(string ruc, string razonSocial, int id)
        {
            var query = from s in context.SOCIOs
                        where s.Ruc.CompareTo(ruc) == 0 &&
                                  s.RazonSocial.ToUpper().CompareTo(razonSocial.ToUpper()) == 0
                                  && s.IdSocio !=id
                        select s;
            return query.Any();
        }

        public bool GetFromSocioByTipoSocioById(int idSocio)
        {
            var query = from s in context.SOCIOs.Include("TIPO_SOCIO")
                        where s.IdSocio.Equals(idSocio)
                        select s;
            return query.Any();
        }
    }
}
